<!-- 详情页 -->
<template>
  <div class="details-page">
    <div class="loading" v-if="showloading">
      <img src="@/assets/images/loading-detail.gif" alt="" />
    </div>
    <div class="details" v-else>
      <div class="details-header" ref="header">
        <!-- 头部内容 -->
        <i
          :class="['iconfont', 'icon-jiantou3', iconBg]"
          onclick="history.back()"
          ref="iconBg"
        ></i>
        <div class="details-tabs" ref="tabs">
          <ul>
            <li
              v-for="(detailsTab, index) in detailsTabs"
              :key="index"
              :class="tabsIndex == index ? 'active' : ''"
              @click="changeIndex(index)"
            >
              {{ detailsTab }}
            </li>
          </ul>
        </div>
        <i :class="['iconfont', 'icon-shoucang1', iconBg]"></i>
        <i :class="iconBg">...</i>
      </div>
      <div class="details-wrap">
        <!-- 商品内容信息 -->
        <div class="details-0 goods-info">
          <mt-swipe :show-indicators="false" @change="handleChange">
            <mt-swipe-item
              v-for="detailsItem in detailsData.gallery_list"
              :key="detailsItem.id"
            >
              <img :src="detailsItem.thumb_url" alt="" />
            </mt-swipe-item>
          </mt-swipe>
          <div class="mt-indicator">
            <strong>{{ current }}</strong
            >/
            <span v-if="detailsData.gallery_list">{{
              detailsData.gallery_list.length
            }}</span>
            <!-- length注意监听的顺序--使用v-if解决 -->
          </div>
          <!-- 商品的内容模块 -->
          <div class="details-content">
            <div class="details-price">
              <div class="shop_price">
                <span
                  ><em>￥</em>
                  <span>{{ detailsData.shop_price | setInt }}</span>
                  <em>{{ detailsData.shop_price | setFloat }}</em>
                </span>
              </div>
              <div class="rebate_price">
                <span
                  ><em>￥</em>
                  <span>{{ detailsData.marketPrice | setInt }}</span>
                  <em>{{ detailsData.marketPrice | setFloat }}</em>
                </span>
              </div>
              <!-- 已售 -->
              <span class="sold_out">已售{{ detailsData.sales_volume }}件</span>
              <div class="details-share" @click="toshareFun">
                <i class="iconfont icon-fenxiang"></i>
                <span>分享</span>
              </div>
            </div>
            <div class="details-title">
              <h3 class="title">
                <em class="tag">自营</em>
                {{ detailsData.goods_name }}
              </h3>
            </div>
          </div>
          <!-- 商品操作内容 -->
          <div class="goods_module_wrap">
            <div class="detail-item" @click="selectBuy">
              <div class="detail-item-left">
                <h3>已选</h3>
              </div>
              <div class="detail-item-right">
                <div class="tips">{{ buyNum }}件</div>
                <i class="iconfont icon-jiantou2"></i>
              </div>
            </div>
            <div class="detail-item" @click="citypopup">
              <div class="detail-item-left">
                <h3>送至</h3>
              </div>
              <div class="detail-item-right">
                <div class="tips">{{ province }}-{{ city }}-{{ area }}</div>
                <i class="iconfont icon-jiantou2"></i>
              </div>
            </div>
            <div class="detail-item">
              <div class="detail-item-left">
                <h3>运费</h3>
              </div>
              <div class="detail-item-right">
                <div class="tips">包邮</div>
              </div>
            </div>
            <div class="detail-item">
              <div class="detail-item-left">
                <h3>服务</h3>
              </div>
              <div class="detail-item-right">
                <div class="tips">支持7天无理由退货1</div>
                <i class="iconfont icon-jiantou2"></i>
              </div>
            </div>
          </div>
        </div>
        <!-- 评论 -->
        <div class="details-1 details-test goods-info">评论</div>
        <!-- 详情 -->
        <div class="details-2 details-test goods-info">详情</div>
        <!-- 推荐 -->
        <div class="details-3 details-test goods-info">推荐</div>
      </div>
      <!-- 加入购物车的弹框 -->
      <div class="select-buy">
        <mt-popup v-model="popupVisible" position="bottom">
          <div class="goods-wrap">
            <div class="goods-img">
              <img :src="detailsData.goods_img" alt="" />
            </div>
            <div class="goods-content">
              <div class="title">{{ detailsData.goods_name }}</div>
              <div class="shop_price">
                <span
                  ><em>￥</em>
                  <span>{{ detailsData.shop_price | setInt }}</span>
                  <em>{{ detailsData.shop_price | setFloat }}</em>
                </span>
              </div>
            </div>
            <i class="iconfont icon-round_delete" @click="closeselectpopup"></i>
          </div>
          <!-- 数量的内容 -->
          <div class="num-wrap">
            <h3>数量</h3>
            <div>
              <button class="minus" @click="buyNum > 1 ? buyNum-- : 1">
                -
              </button>
              <input type="text" v-model="buyNum" class="numtext" />
              <button class="add" @click="buyNum++">+</button>
            </div>
          </div>
          <!-- 加入购物车和立即购买 -->
          <div class="opa-wrap">
            <button class="addCart" @click="addCart">加入购物车</button>
            <button class="buy">立即购买</button>
          </div>
        </mt-popup>
      </div>
      <!-- 城市选择的弹框 -->
      <div class="city-select">
        <mt-popup v-model="popupCityVisible" position="bottom">
          <h3>配送至</h3>
          <i class="iconfont icon-chahao" @click="closepopupCity"></i>
          <!-- 城市选择 -->
          <mt-picker :slots="slots" @change="onValuesChange"></mt-picker>
        </mt-popup>
      </div>
      <!-- 海报的组件显示 -->
      <create-poster
        v-if="showposter"
        :detailsData="detailsData"
        @closePoster="closePosterFun"
      ></create-poster>
      <!-- 底部内容 -->
      <div class="detail-footer">
        <div class="kefu">
          <i class="iconfont icon-kefu1"></i>
          <span>客服</span>
        </div>
        <div class="cart" @click="toCart">
          <i class="iconfont icon-gouwuche"></i>
          <em v-if="totalCount">{{ totalCount }}</em>
          <span>购物车</span>
        </div>
        <button class="addCart" @click="addCart">加入购物车</button>
        <button class="buy">立即购买</button>
      </div>
      <div style="height: 1000px"></div>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { provinces } from "@/mock/city.json";
import CreatePoster from "./components/CreatePoster.vue";
import { DetailsApi } from "@/api/details.js";
let provinceArr = [];
provinces.province.forEach(function (item) {
  provinceArr.push(item.ssqname);
});
console.log(provinceArr);
export default {
  //import引入的组件需要注入到对象中才能使用
  components: { CreatePoster },
  data() {
    //这里存放数据
    return {
      detailsTabs: ["商品", "评论", "详情", "推荐"],
      tabsIndex: 0,
      iconBg: "icon-bg",
      detailsSwiper: [
        {
          id: "1",
          pic: "https://x.dscmall.cn/storage/images/201703/goods_img/0_P_1489108266997.jpg",
        },
        {
          id: "2",
          pic: "https://x.dscmall.cn/storage/images/201703/goods_img/_P_1489108271727.jpg",
        },
        {
          id: "3",
          pic: "https://x.dscmall.cn/storage/images/201703/goods_img/_P_1489108271043.jpg",
        },
        {
          id: "4",
          pic: "https://x.dscmall.cn/storage/images/201703/goods_img/_P_1489108272459.jpg",
        },
      ],
      current: 1,
      detailsData: {
        shopPrice: 1299,
        rebatePrice: 3999,
        title:
          "华帝【年度旗舰】Huawei/华为 Mate 9 6+128GB 4G智能手机限量抢 麒麟960芯片 徕卡双镜头",
        sold_out: 4,
        pic: "https://x.dscmall.cn/storage/images/201703/goods_img/0_P_1489108266997.jpg",
      },
      popupVisible: false,
      buyNum: 1,
      popupCityVisible: false,
      slots: [
        {
          flex: 1,
          values: provinceArr,
          className: "slot1",
          textAlign: "right",
        },
        {
          divider: true,
          content: "-",
          className: "slot2",
        },
        {
          flex: 1,
          values: ["市辖区", "市辖县"],
          className: "slot3",
          textAlign: "center",
        },
        {
          divider: true,
          content: "-",
          className: "slot4",
        },
        {
          flex: 1,
          values: ["请选择"],
          className: "slot5",
          textAlign: "left",
        },
      ],
      province: "请选择",
      city: "请选择",
      area: "请选择",
      showposter: false,
      showloading: true,
    };
  },
  //监听属性 类似于data概念
  computed: {
    totalCount() {
      let count = 0;
      let cartListdatas = this.$store.state.cartListDatas;
      cartListdatas.map((item) => {
        item.children.map((value) => {
          count += value.buyNum;
        });
      });
      console.log(count);
      console.log("执行");
      return count;
    },
  },
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    changeIndex(index) {
      // 样式改变
      this.tabsIndex = index;
      // 元素距离上方的偏移
      // 元素窗口卷进去的距离
      let detailsELe = document.querySelector(".details-" + index);
      // console.log("点击");
      // console.log(detailsELe);
      // console.log(window.pageYOffset);
      // console.log(window.scrollY);
      var backDistance =
        detailsELe.offsetTop -
        window.pageYOffset -
        this.$refs.header.offsetHeight;
      // 缓慢进行滚动
      window.scrollBy({
        top: backDistance,
        behavior: "smooth",
      });
    },
    scrollpageFun() {
      // 头部样式的改变
      var scrollTop = document.documentElement.scrollTop;
      if (scrollTop > 50) {
        this.$refs.header.className = "details-header details-header-scroll";
        this.$refs.tabs.className = "details-tabs details-tabs-scroll";
        this.iconBg = "";
      } else {
        this.$refs.header.className = "details-header";
        this.$refs.tabs.className = "details-tabs";
        this.iconBg = "icon-bg";
      }

      // 关于头部滚动时切换tabs的active
      var detailsAllEle = document.querySelectorAll(".goods-info");
      var headerHeight = this.$refs.header.offsetHeight;
      for (var i = 0; i < detailsAllEle.length; i++) {
        if (window.scrollY + headerHeight + 100 >= detailsAllEle[i].offsetTop) {
          this.tabsIndex = i;
        }
      }
    },
    handleChange(index) {
      this.current = index + 1;
    },
    // 弹出购物车的popup
    selectBuy() {
      this.popupVisible = true;
    },
    closeselectpopup() {
      this.popupVisible = false;
    },
    // 弹出城市的选择框
    citypopup() {
      this.popupCityVisible = true;
    },
    closepopupCity() {
      this.popupCityVisible = false;
    },
    // 当省份选择时触发
    onValuesChange(picker, values) {
      let provincedata = provinces.province;
      // 设置市的数据
      var city = [];
      for (let i = 0; i < provincedata.length; i++) {
        if (values[0] == provincedata[i].ssqname) {
          for (let j = 0; j < provincedata[i].cities.city.length; j++) {
            city.push(provincedata[i].cities.city[j].ssqname);
          }
        }
      }
      picker.setSlotValues(1, city);
      // 设置县的数据
      let area = [];
      for (let i = 0; i < provincedata.length; i++) {
        if (values[0] == provincedata[i].ssqname) {
          for (let j = 0; j < provincedata[i].cities.city.length; j++) {
            if (values[1] == provincedata[i].cities.city[j].ssqname) {
              for (
                let k = 0;
                k < provincedata[i].cities.city[j].areas.area.length;
                k++
              ) {
                area.push(provincedata[i].cities.city[j].areas.area[k].ssqname);
              }
            }
          }
        }
      }

      picker.setSlotValues(2, area);
      // 设置页面上的数据
      this.province = values[0];
      this.city = values[1];
      this.area = values[2];
    },
    // 展示海报
    toshareFun() {
      this.showposter = true;
    },
    // 关闭海报
    closePosterFun(data) {
      this.showposter = data;
    },
    // 添加购物车
    addCart() {
      let CartData = {
        // 店铺Id
        ru_id: this.detailsData.ru_id,
        rz_shop_name: this.detailsData.rz_shop_name,
        isShopSelect: true,
        children: [
          {
            goods_id: this.detailsData.goods_id,
            ru_id: this.detailsData.ru_id,
            goods_thumb: this.detailsData.goods_thumb,
            goods_name: this.detailsData.goods_name,
            shop_price: this.detailsData.shop_price,
            buyNum: this.buyNum,
            isSelect: true,
          },
        ],
      };
      // 提交到vuex,提交mutations
      this.$store.commit("changeCartListDatas", CartData);
      this.popupVisible = false;
    },
    toCart() {
      this.$router.push("/cart");
    },
  },

  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    // 发送数据请求
    console.log(this.$route.params);
    DetailsApi(this.$route.params).then((res) => {
      console.log(res.data);
      // 初始化detailsData
      this.detailsData = res.data.data;
      this.showloading = false;
      document.addEventListener("scroll", this.scrollpageFun);
    });
  },
  beforeDestroy() {
    document.removeEventListener("scroll", this.scrollpageFun);
  },
};
</script>
<style lang="less">
// 局部引入--只是当前vue文件使用
// @import "~@/style/common.less";
.details-page {
  .loading {
    width: 100%;
    height: 66.7rem;
    background-color: #fff;
    // text-align: center;
    // line-height: 66.7rem;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .details {
    .details-header {
      width: calc(100% - 2rem);
      padding: 0 1rem;
      height: 5rem;
      display: flex;
      justify-content: center;
      align-items: center;
      position: fixed;
      top: 0;
      left: 0;
      z-index: 99;
      // background-color: #fff;
      background-color: transparent;
      transition: all 0.5s;
      color: #fff;
      i {
        width: 2.5rem;
        height: 2.5rem;
        text-align: center;
        line-height: 2.5rem;
        margin-right: 1.5rem;
        font-weight: 700;
      }
      .icon-bg {
        background-color: #999;
        border-radius: 50%;
      }
    }
    .details-header-scroll {
      color: #666;
      font-weight: 400;
      background-color: rgba(255, 255, 255, 0.95);
    }

    .details-tabs {
      width: 70%;
      opacity: 0;
      ul {
        display: flex;
        li {
          flex: 1;
          font-size: 1.5rem;
          color: #555;
          position: relative;
          text-align: center;
        }
        .active {
          color: #000;
          font-weight: 700;
          &::after {
            content: "";
            position: absolute;
            bottom: -0.5rem;
            left: 1.8rem;
            width: 2.6rem;
            height: 0.3rem;
            background: linear-gradient(
              90deg,
              #f91f28,
              rgba(255, 79, 46, 0.35)
            );
          }
        }
      }
    }
    .details-tabs-scroll {
      opacity: 1;
    }
    .details-wrap {
      // 商品的内容
      .details-0 {
        .mint-swipe {
          width: 100%;
          height: 37.5rem;
          position: relative;
          img {
            width: 100%;
            height: 37.5rem;
          }
        }
        // 指示多少内容
        .mt-indicator {
          position: absolute;
          right: 0;
          top: 30rem;
          width: 5rem;
          height: 2.5rem;
          line-height: 2.5rem;
          border-top-left-radius: 1.25rem;
          border-bottom-left-radius: 1.25rem;
          text-align: center;
          color: #fff;
          font-size: 1.4rem;
          background-color: rgba(41, 47, 54, 0.4);
          z-index: 10;
        }
        .details-content {
          width: calc(100% - 2rem);
          padding: 1rem;
          background-color: #fff;
          border-radius: 0 0 1rem 1rem;
          .details-price {
            height: 5.9rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            .shop_price {
              font-size: 2.4rem;
              color: #f91f28;
            }
            .rebate_price {
              margin-left: -5rem;
              text-decoration: line-through;
              color: #999;
            }
            .sold_out {
              margin-left: -5rem;
              font-size: 1.4rem;
              color: #999;
            }
            // 分享
            .details-share {
              width: 4rem;
              height: 4rem;
              display: flex;
              margin-right: 0rem;
              flex-direction: column;
              font-size: 1.2rem;
              justify-content: center;
              align-items: center;
            }
          }
          .details-title {
            .title {
              font-size: 1.5rem;
              line-height: 1.5rem;
              // em {
              //   font-size: 1.2rem;
              //   padding: 0.1rem 0.4rem;
              //   background: linear-gradient(90deg, #f91f28, #ff4e2e);
              //   border-radius: 1.2rem;
              //   color: #fff;
              // }
              .tag {
                // 混入
                // .tag;
                // 命名空间
                .namespace > .tag;
              }
            }
          }
        }

        // 已选等
        .goods_module_wrap {
          width: calc(100% - 2rem);
          padding: 1rem;
          background: #fff;
          border-radius: 1rem;
          margin-top: 1rem;
          font-size: 1.4rem;
          .detail-item {
            display: flex;
            height: 5rem;
            justify-content: space-between;
            align-items: center;
            .detail-item-left {
              width: 15%;
              h3 {
                font-size: 1.6rem;
              }
            }
            .detail-item-right {
              width: 85%;
              height: 4rem;
              display: flex;
              justify-content: space-between;
              align-items: center;
              font-size: 1.4rem;
              border-bottom: 1px solid #efefef;
            }
          }
        }
      }
      .details-test {
        height: 60rem;
        background-color: pink;
        border: 1px solid red;
        margin-top: 2rem;
      }
    }
    .select-buy {
      .mint-popup {
        width: 100%;
        height: 27.7rem;
        border-radius: 1.5rem 1.5rem 0 0;
        background-color: #fff;
        .goods-wrap {
          width: calc(100% - 2rem);
          height: 12.8rem;
          padding: 1rem;
          display: flex;
          margin-top: 1rem;
          img {
            width: 9rem;
            height: 9rem;
          }
          .title {
            width: 24rem;
            height: 5.8rem;
            font-size: 1.4rem;
            margin-top: 1rem;
            // 多行文本省略号
            //   多行文本省略号
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            overflow: hidden;
          }
          .shop_price {
            color: #e22c2c;
            font-weight: 700;
            span {
              font-size: 1.5rem;
              em {
                font-size: 1.05rem;
              }
            }
          }
        }
        .num-wrap {
          display: flex;
          width: calc(100% - 2rem);
          height: 5rem;
          margin: 1rem;
          justify-content: space-between;
          align-items: center;
          h3 {
            font-size: 1.6rem;
          }
          div {
            button {
              width: 2rem;
              height: 2.6rem;
            }
            .numtext {
              width: 4rem;
              height: 2.6rem;
              text-align: center;
              background-color: #ccc;
            }
          }
        }
        .opa-wrap {
          button {
            width: 17rem;
            height: 4.2rem;
            margin: 0 0.5rem;
            border: none;
            font-size: 14px;
            font-weight: 700;
            color: #fff;
            border-radius: 2rem;
          }
          .addCart {
            background-color: #ffc707;
          }
          .buy {
            margin-left: 1rem;
            background-color: #f91f28;
          }
        }
      }
    }
    .city-select {
      position: relative;
      .mint-popup {
        width: 100%;
        height: 27.7rem;
        border-radius: 1.5rem 1.5rem 0 0;
        background-color: #fff;
        h3 {
          font-size: 1.6rem;
          text-align: center;
          color: #282828;
          font-weight: 700;
          margin-top: 1rem;
        }
        .icon-chahao {
          position: absolute;
          right: 2rem;
          top: 2rem;
        }
      }
    }
    .detail-footer {
      height: 4.9rem;
      border-top: 1px solid #ccc;
      width: 100%;
      display: flex;
      position: fixed;
      bottom: 0;
      left: 0;
      background: #fff;
      z-index: 99;
      justify-content: center;
      align-items: center;
      text-align: center;
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        i {
          font-size: 2rem;
        }
      }
      button {
        border-radius: 2.2rem;
        font-weight: bold;
        height: 4.2rem;
        width: 12.5rem;
        margin-right: 1rem;
      }
      .addCart {
        background: #ffc707;
        color: #fff;
      }
      .buy {
        background: #f55;
        color: #fff;
      }
      .cart {
        position: relative;
        em {
          position: absolute;
          height: 1.5rem;
          min-width: 1.5rem;
          line-height: 1.5rem;
          font-size: 1rem;
          background: #fff;
          border: 0.1rem solid #f55;
          color: #f55;
          border-radius: 1rem;
          right: 0.5rem;
          top: -0.5rem;
        }
      }
    }
  }
}
</style>